/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class SpellAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<String> groups;
	private ArrayList<ArrayList<Spell>> Spell;
	private LayoutInflater inflater;

	public SpellAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<Spell>> colors) {
		this.context = context;
		this.groups = groups;
		this.Spell = Spell;
		inflater = LayoutInflater.from(context);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return Spell.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return (long) (groupPosition * 1024 + childPosition); // Max 1024 children per group
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		View v = null;
		if (convertView != null)
			v = convertView;
		else
			v = inflater.inflate(R.layout.spell_child_row, parent, false);
		Spell c = (Spell) getChild(groupPosition, childPosition);
		TextView spellName = (TextView) v.findViewById(R.id.battlemagicSpellChildTxt);
		if (spellName != null)
			spellName.setText(c.getName());
		//		TextView rgb = (TextView)v.findViewById( R.id.rgb );
		//		if( rgb != null )
		//			rgb.setText( c.getRgb() );
		//		CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
		//        cb.setChecked( c.getState() );
		return v;
	}

	public int getChildrenCount(int groupPosition) {
		return Spell.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return (long) (groupPosition * 1024); // To be consistent with getChildId
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		View v = null;
		if (convertView != null)
			v = convertView;
		else
			v = inflater.inflate(R.layout.spell_group_row, parent, false);
		String spellGroup = (String) getGroup(groupPosition);
		TextView spellLevel = (TextView) v.findViewById(R.id.battlemagicSpellRowLevelTxt);
		String spellsRemainingGroup = (String) getGroup(groupPosition);
		TextView spellsRemaining = (TextView) v.findViewById(R.id.battlemagicSpellRowRemainingTxt);
		if (spellGroup != null && spellsRemainingGroup != null)
			spellLevel.setText(spellGroup);
		spellsRemaining.setText(spellsRemainingGroup);
		return v;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void onGroupCollapsed(int groupPosition) {
	}

	public void onGroupExpanded(int groupPosition) {
	}

}
